/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package attendance.web.servlets;

import attendance.libs.Attendance;
import attendance.libs.AttendanceImplDb;
import attendance.libs.Seminar;
import attendance.libs.Subject;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author papi
 */
public class AddSeminar extends HttpServlet {

    /**
     *              It shows a form to add seminar.
     *
     * @param       request
     * @param       response
     * @throws      javax.servlet.ServletException
     * @throws      java.io.IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        ResourceBundle text = ResourceBundle.getBundle("attendance.web.data.WebLang");

        Attendance attendance = new AttendanceImplDb();

        // Show data.
        request.setAttribute("pageTitle", text.getString("SEMINAR_INSERTING"));
        request.setAttribute("subjectCode", text.getString("SUBJECT_CODE"));
        request.setAttribute("subjectName", text.getString("SUBJECT_NAME"));
        request.setAttribute("seminarNumber", text.getString("SEMINAR_NUMBER"));
        //request.setAttribute("error", request.getAttribute("error"));
        request.getRequestDispatcher("/seminarForm.jsp").forward(request, response);
    }

    /**
     *              It processes data from form.
     *
     * @param       request
     * @param       response
     * @throws      javax.servlet.ServletException
     * @throws      java.io.IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        ResourceBundle text = ResourceBundle.getBundle("attendance.web.data.WebLang");

        // Variables which are important to insert new seminar into database.
        String subjectName = request.getParameter("subjectName");
        String subjectCode = request.getParameter("subjectCode");
        Integer seminarNum = null;

        try {
            seminarNum = Integer.valueOf(request.getParameter("seminarNum"));
        } catch (NumberFormatException e) {
            request.setAttribute("error", text.getString("WRONG_SEMINAR_NUM"));
            doGet(request, response);
            return;
        }

        // Checking if the data are not empty
        if (subjectName == null || subjectName.length() == 0) {
            request.setAttribute("error", text.getString("WITHOUT_SUBJECT_NAME"));
            doGet(request, response);
            return;
        }
        if (subjectCode == null || subjectCode.length() == 0) {
            request.setAttribute("error", text.getString("WITHOUT_SUBJECT_CODE"));
            doGet(request, response);
            return;
        }

        // Data inserting.
        Attendance attendance = new AttendanceImplDb();
        if (!attendance.addSeminar(new Seminar(new Subject(subjectName, subjectCode), seminarNum))) {
            request.setAttribute("error", text.getString("SEMINAR_ALREADY_EXISTS"));
            doGet(request, response);
            return;
        }
       
        request.getRequestDispatcher("SeminarList").forward(request, response);

    }

}
